/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.*;
import org.ofbiz.entity.util.*;
import org.ofbiz.entity.condition.*;

addressContactMechId = null;
address1 = null;
city = null;
stateProvinceGeoId = null;
countryGeoId = null;
postalCode = null;
telephoneContactMechId = null;
emailAddress = null;
roleTypeId = null;
userLoginId = null;

customer = [:];
customerGPSList = [];

if (parameters.partyId) {
    //Address
    partyBillAddressResults = dispatcher.runSync("getPartyPostalAddress", [partyId : parameters.partyId, userLogin : userLogin]);
    if (partyBillAddressResults) {
        addressContactMechId = partyBillAddressResults.contactMechId;
        address1 = partyBillAddressResults.address1;
        city = partyBillAddressResults.city;
        stateProvinceGeoId = partyBillAddressResults.stateProvinceGeoId;
        countryGeoId = partyBillAddressResults.countryGeoId;
        postalCode = partyBillAddressResults.postalCode;
    }
    //PhoneNumber
    partyTelephoneResults = dispatcher.runSync("getPartyTelephone", [partyId : parameters.partyId, userLogin : userLogin]);
    if (partyTelephoneResults) {
        if(UtilValidate.isNotEmpty(partyTelephoneResults.contactMechId)){
            telephoneContactMechId = partyTelephoneResults.contactMechId;
            customer.contactNumber = partyTelephoneResults.contactNumber;
        }
    }
    
    //Email
    partyEmailResults = dispatcher.runSync("getPartyEmail", [partyId : parameters.partyId, userLogin : userLogin]);
    if (partyEmailResults) {
        if(UtilValidate.isNotEmpty(partyEmailResults.contactMechId)){
            customer.emailAddress = partyEmailResults.emailAddress;
        }
    }
    //Role
    roleTypeIds = [];
    roleTypeList = delegator.findByAnd("RoleType", [parentTypeId: "GPS_ROLE"], null, false);
    roleTypeList.each { roleType ->
        roleTypeIds.add(roleType.roleTypeId);
    }
    cond = EntityCondition.makeCondition("roleTypeId" , EntityOperator.IN, roleTypeIds);
    cond = EntityCondition.makeCondition([cond, EntityCondition.makeCondition([partyId : parameters.partyId])]);
    partyRoleList = delegator.findList("PartyRole", cond , null, ["-lastUpdatedStamp"], null, false);
    if (partyRoleList) {
        roleTypeId = EntityUtil.getFirst(partyRoleList).roleTypeId;
    }
    //UserLogin
    userLoginList = delegator.findByAnd("UserLogin", [partyId: parameters.partyId], null, false);
    userLoginId = EntityUtil.getFirst(userLoginList).userLoginId;
    
    /*** Customer GPS List ***/
    customerGPSList = delegator.findByAnd("ProductRoleDetail", [roleTypeId: "PRODUCT_OWNER", partyId: parameters.partyId], null, false);
    if (customerGPSList) {
        customerGPSList = EntityUtil.filterByDate(customerGPSList);
    }
}
customer.partyId = parameters.partyId;
customer.addressContactMechId = addressContactMechId;
customer.address1 = address1;
customer.city = city;
customer.stateProvinceGeoId = stateProvinceGeoId;
customer.countryGeoId = countryGeoId;
customer.postalCode = postalCode;
customer.telephoneContactMechId = telephoneContactMechId;
customer.roleTypeId = roleTypeId;
customer.userLoginId = userLoginId;
context.customer = customer;
context.customerGPSList = customerGPSList;